#!/bin/bash

function startuptomcat(){

if [ -d "${tomcat_home/webapps/clinicSystem}" ];then

datethen=`date +%Y-%m-%d-%H-%M`

cd $tomcat_home/webapps && tar -cf clinicSystem$datethen.tar clinicSystem --remove-files && mv clinicSystem$datethen.tar /home/wudan

fi

mkdir -p $tomcat_home/webapps/clinicSystem

cp -a /home/wudan/jenkinsWorkspace/workspace/248clinicSystem/WebContent/* $tomcat_home/webapps/clinicSystem


echo "wudan"|sudo -S $tomcat_home/bin/startup.sh
#su - root<<EOF
#XXXXXXX;
#EOF
#$tomcat_home/bin/startup.sh;

}


tomcat_home='/home/wudan/apache-tomcat-7.0.70'


pidlist=`ps -ef|grep tomcat|grep -w $tomcat_home|grep -v grep|awk '{print $2}'`

if [ ! -n "${pidlist}" ];then
echo "pidlist is $pidlist"
echo 'TOMCAT is not started'
startuptomcat

pidnew=`ps -ef|grep tomcat|grep -w $tomcat_home|grep -v grep|awk '{print $2}'`


echo "pidnew now is $pidnew"
exit 0
fi

echo "pidlist is $pidlist"

for i in $pidlist;do

echo 'Begin TO kill tomcat process'

echo "wudan"|sudo -S kill -9 $i

done

startuptomcat

pidnew=`ps -ef|grep tomcat|grep -w $tomcat_home|grep -v grep|awk '{print $2}'`

echo "pidnew now is $pidnew"


exit 0
